package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.autocode.ZlTemplateDtlMapper;
import com.my.zl.console.dto.autocode.templateDtl.ReqTemplateDtlAddDTO;
import com.my.zl.console.dto.autocode.templateDtl.ReqTemplateDtlDelDTO;
import com.my.zl.console.dto.autocode.templateDtl.ReqTemplateDtlDetailDTO;
import com.my.zl.console.dto.autocode.templateDtl.ReqTemplateDtlEditDTO;
import com.my.zl.console.dto.autocode.templateDtl.ReqTemplateDtlQueryDTO;
import com.my.zl.console.dto.autocode.templateDtl.ReqTemplateDtlStateDTO;
import com.my.zl.console.dto.autocode.templateDtl.ResTemplateDtlAddDTO;
import com.my.zl.console.dto.autocode.templateDtl.ResTemplateDtlDelDTO;
import com.my.zl.console.dto.autocode.templateDtl.ResTemplateDtlDetailDTO;
import com.my.zl.console.dto.autocode.templateDtl.ResTemplateDtlEditDTO;
import com.my.zl.console.dto.autocode.templateDtl.ResTemplateDtlQueryDTO;
import com.my.zl.console.dto.autocode.templateDtl.ResTemplateDtlStateDTO;
import com.my.zl.console.dto.autocode.templateDtl.ResTemplateDtlVO2;
import com.my.zl.console.entity.autocode.ZlTemplateDtl;
import com.my.zl.console.po.autocode.templateDtl.ZlTemplateDtlVo;
import com.my.zl.console.service.autocode.ITemplateDtlService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 模板明细
 * @author ZL
 */
@Service
public class TemplateDtlServiceImpl extends BaseManager implements ITemplateDtlService {

    @Autowired
    private ZlTemplateDtlMapper templateDtlMapper;

    @Override
    public ResTemplateDtlQueryDTO queryTemplateDtlList(ReqTemplateDtlQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResTemplateDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResTemplateDtlQueryDTO.class);
        if(StringUtils.isNotBlank(reqInfo.getFileTitle())){
            reqInfo.setFileTitle("%"+reqInfo.getFileTitle()+"%");
        }
        if(StringUtils.isNotBlank(reqInfo.getFileName())){
            reqInfo.setFileName("%"+reqInfo.getFileName()+"%");
        }
        if(StringUtils.isNotBlank(reqInfo.getQuoteTag())){
            reqInfo.setQuoteTag("%"+reqInfo.getQuoteTag()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlTemplateDtlVo> list = templateDtlMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResTemplateDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResTemplateDtlDetailDTO queryTemplateDtlDetail(ReqTemplateDtlDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResTemplateDtlDetailDTO res = new ResTemplateDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlTemplateDtl view = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResTemplateDtlAddDTO insertTemplateDtlInfo(ReqDTO<ReqTemplateDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlAddDTO reqData = reqInfo.getReqData();
        ResTemplateDtlAddDTO res = new ResTemplateDtlAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlTemplateDtl record = new ZlTemplateDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setTemplateFileId(IdUtil.simpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateFileId",record.getTemplateFileId());
        //检查主键是否冲突
        if(templateDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        templateDtlMapper.insertSelective(record);
        
        ZlTemplateDtl keyInfo = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTemplateDtlEditDTO updateTemplateDtlInfo(ReqDTO<ReqTemplateDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlEditDTO reqData = reqInfo.getReqData();
        ResTemplateDtlEditDTO res = new ResTemplateDtlEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlTemplateDtl record = new ZlTemplateDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        templateDtlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateFileId",record.getTemplateFileId());
        ZlTemplateDtl keyInfo = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTemplateDtlDelDTO deleteTemplateDtlInfo(ReqDTO<ReqTemplateDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlDelDTO reqData = reqInfo.getReqData();
        ResTemplateDtlDelDTO res = new ResTemplateDtlDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        templateDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResTemplateDtlStateDTO updateTemplateDtlState(ReqDTO<ReqTemplateDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlStateDTO reqData = reqInfo.getReqData();
        ResTemplateDtlStateDTO res = new ResTemplateDtlStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlTemplateDtl record = new ZlTemplateDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        templateDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateFileId",record.getTemplateFileId());
        ZlTemplateDtl keyInfo = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}